package com.systemic.controller;

import com.systemic.core.ResponseResult;
import com.systemic.pojo.MindfulnessClass;
import com.systemic.pojo.UserInfo;
import com.systemic.service.UserInfoService;
import com.systemic.vo.PageVo;
import com.systemic.vo.UserAvatar;
import com.systemic.vo.UserBackground;
import com.systemic.vo.UserItem;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName: UserController
 * @Description: 用户管理
 * @Author:
 * @CreateTime: 2023-12-27 15:25
 * @Version:
 **/

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserInfoService userInfoService;

    @PostMapping("/userList")
    public ResponseResult<List<UserInfo>> userList(@RequestBody PageVo pageVo){
        List<UserInfo> res = userInfoService.selectListPage(pageVo);
        return !res.isEmpty() ? ResponseResult.success(res) : ResponseResult.failed();
    }

    /**
     * 修改用户信息
     * @param userInfo
     */
    @PostMapping("/uploadUserInfo")
    public ResponseResult<String> updateUserInfo(@RequestBody UserInfo userInfo){
        userInfoService.updateUserInfo(userInfo);
        return ResponseResult.success("修改成功");
    }

    /**
     * 查询用户item
     * @param uid
     * @return
     */
    @GetMapping("/userItem/{uid}")
    public ResponseResult<UserItem> getUserItem(@PathVariable("uid") Integer uid){
        UserItem item =userInfoService.selectUserItem(uid);
        return item != null ? ResponseResult.success(item) : ResponseResult.failed("未查询到");
    }

    /**
     * 获取用户信息
     * @param uid
     * @return
     */
    @GetMapping("/info/{uid}")
    public ResponseResult<UserInfo> getUserInfo(@PathVariable("uid") Integer uid){
        UserInfo info = userInfoService.selectUserInfoByUid(uid);
        return ResponseResult.success(info);
    }


    /**
     * 获取用户头像
     * @param uid
     * @return
     */
    @GetMapping("/getAvatar/{uid}")
    public ResponseResult<UserAvatar> getUserAvatar(@PathVariable("uid") Integer uid){
        UserInfo info = userInfoService.selectUserInfoByUid(uid);
        UserAvatar res = new UserAvatar();
        res.setUid(info.getUid());
        res.setAvatar(info.getAvatar());
        return ResponseResult.success(res);
    }


    /**
     * 获取用户壁纸
     * @param uid
     * @return
     */
    @GetMapping("/getBackground/{uid}")
    public ResponseResult<UserBackground> getBackground(@PathVariable("uid") Integer uid){
        UserInfo info = userInfoService.selectUserInfoByUid(uid);
        UserBackground res = new UserBackground();
        res.setUid(info.getUid());
        res.setBackground(info.getBackground());
        return ResponseResult.success(res);
    }



    /**
     * 上传头像
     * @param file
     * @param uid
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadAvatar")
    public ResponseResult<String> uploadAvatar(@RequestParam("file") MultipartFile file,
                                               @RequestParam("uid") Integer uid) throws IOException {
        if (!file.isEmpty()) {
            System.out.println("尝试上传uid为" + uid + "用户的头像");
            userInfoService.uploadAvatar(file, uid);
            return ResponseResult.success("上传成功");
        }
        System.out.println("uid："+ uid +"的用户上传头像失败");
        return ResponseResult.failed("上传失败");
    }



    /**
     * 上传壁纸
     * @param file
     * @param uid
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadBackground")
    public ResponseResult<String> uploadBackground(@RequestParam("file") MultipartFile file,
                                               @RequestParam("uid") Integer uid) throws IOException {
        if (!file.isEmpty()) {
            System.out.println("尝试上传uid为" + uid + "用户的壁纸");
            userInfoService.uploadBackground(file, uid);
            return ResponseResult.success("上传成功");
        }
        System.out.println("uid："+ uid +"的用户上传壁纸失败");
        return ResponseResult.failed("上传失败");
    }


}
